<template>
    <el-container class="tl-height-100-percent">
        <el-header>
            <div>
                <img class="tl-text-no-select" src="src/assets/images/heima.png" alt="logo">
                <span class="tl-text-no-select">博客后台管理</span>
            </div>
            <el-dropdown placement="bottom">
                <el-avatar style="border: #a1a3a9 solid 1px; box-shadow: 0 0 3px #a1a3a9" :size="40" :src="this.$store.state.avatar_url"></el-avatar>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item icon="iconfont icon-writing-blogs">编写博客</el-dropdown-item>
                    <el-dropdown-item icon="iconfont icon-my-blog">我的博客</el-dropdown-item>
                    <el-dropdown-item @click.native="logout" icon="iconfont icon-logout" divided>注销</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container style="height: 500px">
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button tl-vh-center" @click="toggleCollapse"><i class="iconfont icon-fold"></i></div>
                <!-- 侧边栏菜单区域 -->
                <el-menu :default-active="$route.path" unique-opened background-color="#333744" text-color="#fff"
                         active-text-color="#409EFF" :collapse="isCollapse" :collapse-transition="false" router>
                    <el-submenu v-for="item in menuList" :key="item.id" :index="item.id + ''">
                        <template slot="title">
                            <i class="tl-font-18-px" :class="iconsObject[item.id]"></i>
                            <span class="tl-font-16-px tl-margin-left-5" slot="title">{{ item.name }}</span>
                        </template><!--"'/' + subItem.path"-->
                        <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="subItem.path">
                            <i class="tl-font-18-px" :class="iconsObject[subItem.id]"></i>
                            <span class="tl-font-16-px tl-margin-left-5" slot="title">{{ subItem.name }}</span>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧主题区域 -->
            <el-main>
                <transition name="el-fade-in-linear el-zoom-in-center">
                    <router-view></router-view>
                </transition>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    name: 'Home',
    data: function () {
        return {
            menuList: [],
            iconsObject: {
                1: 'iconfont icon-manage-blog',
                101: 'iconfont icon-my-blog',
                102: 'iconfont icon-writing-blogs',
                2: 'iconfont icon-tubiao-',
                201: 'iconfont icon-fenlei',
                3: 'iconfont icon-log-manage',
                301: 'iconfont icon-admin-system-log',
                302: 'iconfont icon-admin-login-log-list'
            },
            isCollapse: false
        }
    },
    created: function () {
        this.getMenuList();
    },
    methods: {
        logout: function () {
            this.$confirm('此操作将退出系统, 是否继续?', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(() => {
                this.$message({ showClose: true, type: 'success', message: '注销成功!' });
                window.localStorage.removeItem('JWT_TOKEN');
                this.$router.push('/web/login');
            }).catch(() => {
                this.$message({ showClose: true, type: 'info', message: '已取消注销' });
            });
        },
        getMenuList: async function () {
            this.menuList = [{
                id: 1,
                name: '博客管理',
                children: [{
                    id: 101,
                    name: '博客列表',
                    path: '/admin/blogs'
                }, {
                    id: 102,
                    name: '编写博客',
                    path: '/admin/blog/add'
                }]
            }, {
                id: 2,
                name: '分类管理',
                children: [{
                    id: 201,
                    name: '分类列表',
                    path: '/admin/types'
                }]
            }, {
                id: 3,
                name: '日志管理',
                children: [{
                    id: 301,
                    name: '系统日志',
                    path: '/admin/logs'
                }, {
                    id: 302,
                    name: '登入日志',
                    path: '/admin/logs/login'
                }]
            }]
        },
        // 点击按钮切换菜单的折叠与展开
        toggleCollapse: function () {
            this.isCollapse = !this.isCollapse;
        }
    }
}
</script>

<style scoped>
.iconfont {
    text-align: center;
    display: inline-block;
    width: 24px;
}

.el-header {
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
    font-size: 20px;
}

.el-menu {
    border-right: 0;
}

.el-header > div {
    display: flex;
    align-items: center;
}

.el-header > div span {
    margin-left: 15px;
}

.el-aside {
    background-color: #333744;
}

.el-main {
    background-color: #EAEDF1;
}

.toggle-button {
    background-color: #4A5064;
    height: 20px;
    cursor: pointer;
}

.toggle-button .icon-fold {
    font-size: 20px;
    color: #ddd;
}
</style>
